package com.pr.makfilm.server;

import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.domain.User;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IUserService;
import org.apache.log4j.Logger;
import org.apache.wicket.Session;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.request.Request;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 08.09.13
 * Time: 15:02
 * To change this template use File | Settings | File Templates.
 */
public class MainSession extends AuthenticatedWebSession
{
   public static final Logger LOG = Logger.getLogger(MainSession.class);

   @SpringBean(name = "authenticationManager")
   private AuthenticationManager authenticationManager;

   @SpringBean
   private ISecurityService securityService;

   @SpringBean
   private IUserService userService;

   public MainSession(Request request)
   {
      super(request);
      Injector.get().inject(this);
   }

   @Override
   public boolean authenticate(String login, String password)
   {
      boolean authenticated = false;
      try
      {
         Authentication authentication = authenticationManager.authenticate(
                 new UsernamePasswordAuthenticationToken(
                         login,
                         password));
         SecurityContextHolder.getContext().setAuthentication(authentication);
         authenticated = authentication.isAuthenticated();
      }
      catch (AuthenticationException e)
      {
         authenticated = false;
         LOG.error("Authentication error! " + e.getMessage());
      }

      return authenticated;
   }

   public static MainSession get()
   {
      return (MainSession) Session.get();
   }

   @Override
   public Roles getRoles()
   {
      Roles roles = new Roles();
      getRolesIfSignedIn(roles);
      return roles;
   }

   @Override
   public void invalidate()
   {
      super.invalidate();
      SecurityContextHolder.getContext().setAuthentication(null);
   }

   private void getRolesIfSignedIn(Roles roles)
   {
      if (isSignedIn())
      {
         addRolesFromAuthentication(roles);
      }
   }

   private void addRolesFromAuthentication(Roles roles)
   {
      User loggedInUser = securityService.getLoggedInUser();
      if (isSignedIn() && loggedInUser != null)
      {
         roles.add(loggedInUser.getType().toString());
      }
   }
}
